package com.speedricemanager.other.server;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

import com.jbm.core.client.MainCallback;
import com.jbm.jsm.common.OHInterface;
import com.speedricemanager.R;
import com.speedricemanager.other.config.Config;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class ServerModel {
    private static OHInterface net;

    public static OHInterface getInterface(){
        net= OHInterface.getG_server();
        return net;
    }

    public static Integer getLoginID(){
      return  OHInterface.getG_server().getM_local_id();
    }



    public void init(final Context context, final MainCallback call){
        getInterface();
        Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                String strfile = Environment.getExternalStorageDirectory() + "/Downs/";
                boolean  ISConn= net.Init(null,strfile,"server_log", Config.IP, (short) 1910, "app_client",call);//建立连接
                net.setM_temppath(strfile);
                subscriber.onNext(ISConn);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<Boolean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("main","连接服务器结果 错误了"+e.getMessage());
                Intent in = new Intent();
                in.setAction("server");
                in.putExtra("success", "1");
                context.sendBroadcast(in);
            }

            @Override
            public void onNext(Boolean aBoolean) {
                Intent in = new Intent();
                Log.e("main","服务器返回");
                in.setAction("server");
                if(aBoolean==true) {
                    in.putExtra("success", "0");
                }else{
                    in.putExtra("success", "1");
                }
                context.sendBroadcast(in);
            }


        });
    }


    /**
     * 启动通知
     * @param title         标题
     * @param content    内容
     * @param type 类型 0代表无声 1代表有声
     * @return
     */
    public Notification getNotification(String title, String content, int type, Context context, Integer account_id){
//        Intent notificationIntent = new Intent(context, HomeAty.class);
        //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//        Buddy_ListData data=new Buddy_ListData();
//        data.setM_account_id(4);
//        data.setNickname("aaaaa");
//        notificationIntent.putExtra("main",data);
//        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//        notificationIntent.putExtra("isMessage",true);
//        PendingIntent pendingIntent = PendingIntent.getActivity(context,12346, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = null;

        switch (type){
            case 0:
                notification = new Notification.Builder(context)
                        .setContentTitle(title)
                        .setContentText(content)
                        .setSmallIcon(R.mipmap.ic_launcher)
//                        .setAutoCancel(true)
//                        .setContentIntent(pendingIntent)
                        .build();
                break;
            case 1:
                notification = new Notification.Builder(context)
                        .setContentTitle(title)
                        .setContentText(content)
//                        .setAutoCancel(true)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setDefaults(Notification.DEFAULT_SOUND)
//                        .setContentIntent(pendingIntent)
                        .build();

                break;
        }


        return notification;
    }


}
